<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\system;


use app\admin\model\website\Forbidden as ForbiddenModel;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use app\common\service\TenImService;
use broadcast\redis\setRedis;
use app\admin\model\SystemWebsite;
use Exception;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="站点管理")
 */
class Forbidden extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'create_time'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new ForbiddenModel();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $website = setRedis::getCache(session("website"),'info');
            if (empty($website)) {
                $website = SystemWebsite::where("website_ident",session("website"))->find();
                if ($website) $website = $website->toArray();
                setRedis::setCache(session("website"),"info",$website,0);
            }
            try{
                $list = TenImService::getGroupShuttedUin(['GroupId'=>$website['chat']]);
                $count =  count($list['ShuttedUinList']);
                $list  = $list['ShuttedUinList'];
                foreach ($list as &$item){
                    $item['ShuttedUntil'] = date('Y-m-d H:i',$item['ShuttedUntil']);
                    $item['account'] = spliceStr($item['Member_Account'],'_W_zhibo1_U_','');
                }
            }catch(Exception $e) {
                $count = 0;
                $list = [];         
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {

        if ($this->request->isAjax()) {
            $post = $this->request->post();

           
            $post['operation'] = session("admin.username");


            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $post = $this->request->post();
        $row = $this->model->find($post['id']);
        empty($row) && $this->error('数据不存在');
        try {
            $row->save([
                $post['field'] => $post['value'],
            ]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

    public function recovery($Member_Account) 
    {
        // $row = $this->model->whereIn('id', $Member_Account)->select();
        // $row->isEmpty() && $this->error('数据不存在');
        // try {
        //     $save = $row->delete();
        // } catch (\Exception $e) {
        //     $this->error('恢复失败');
        // }

        try{
            $website = setRedis::getCache(session("website"),'info');
            if (empty($website)) {
                $website = SystemWebsite::where("website_ident",session("website"))->find();
                if ($website) $website = $website->toArray();
                setRedis::setCache(session("website"),"info",$website,0);
            }
            $params = [
                'GroupId' => $website['chat'],
                "ShutUpTime" => 0,
                'Members_Account' => [
                    $Member_Account
                ]
            ];
            $save = TenImService::forbidSendMsg($params);
        }catch(Exception $e) {
            $this->error("恢复失败");
        }
        $save ? $this->success('恢复成功') : $this->error('恢复失败');
    }


}
